<?php
/************************************************************************/
/* FrameWork  test preview                                              */
/* ==================================================================== */
/*                                                                      */
/* This program is free software. You can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 2 of the License.       */
/************************************************************************/

if(!user_getuser())die();

$author=user_getuser();
//upload files
	    if (move_uploaded_file($_FILES['file']['tmp_name'], "datas/applications/".apptitle($appname)."/images/".$_FILES['file']['name'])) {
		if ($_FILES['file']['size']>50000 || strtolower(substr($_FILES['file']['name'],-4))==".php")
		    unlink("datas/applications/".apptitle($appname)."/images/".$_FILES['file']['name']);
		else
		    chmod("datas/applications/".apptitle($appname)."/images/".$_FILES['file']['name'],0644);
		    print ("file upload") ;
	    }
//--------------------------------

import_request_variables('cpg','');

    if ($page==""){
	$page="FrontPage";
    }
    else{
	$page=str_replace('..',"",$page);
	$page=str_replace('/',"",$page);
    }


    if (file_exists("datas/applications/".apptitle($appname)."/pages/$page")){
	$ta=file("datas/applications/".apptitle($appname)."/pages/$page");
	$datepage=filemtime("datas/applications/".apptitle($appname)."/pages/$page");
    }


    $ta_text="";
    for($i=1;$i<count($ta);$i++)
	$ta_text=$ta_text.trim(rawurldecode($ta[$i]));


?>

<script type="text/javascript" src="extras/FCKeditor/fckeditor.js"></script>
<script type="text/javascript">
    var sBasePath ="extras/FCKeditor/";

    var editor = new FCKeditor( 'ta' ) ;
    editor.BasePath	= sBasePath ;
    editor.Height	= 300 ;
    editor.Value	= "<?=$ta_text ?>";

    function insertHTML() {
	var oEditor = FCKeditorAPI.GetInstance('ta') ;
	var html = prompt("Enter some HTML code here");
	if (html) {
	    oEditor.InsertHtml(html);
	}
    }

    function newwikipage() {
	var oEditor = FCKeditorAPI.GetInstance('ta') ;
	var pagename = prompt("Enter the name of new page" );
	var html="<a href='index.php?appname=<?=$appname?>&page="+pagename+"'>"+pagename+"</a>\n";
	if (pagename) {
	    oEditor.InsertHtml(html);
	}
    }

    function newlink() {
	var oEditor = FCKeditorAPI.GetInstance('ta') ;
	var pageindex= form1.select1.selectedIndex;
	var pagename = form1.select1.options[pageindex].text;
	var html="<a href='index.php?appname=<?=$appname?>&page="+pagename+"'>"+pagename+"</a>\n";
	if (pagename) {
	    oEditor.InsertHtml(html);
	}
    }

    function newimage() {
	var oEditor = FCKeditorAPI.GetInstance('ta') ;
	var index= form1.select2.selectedIndex;
	var name = form1.select2.options[index].text;
	var html="<img src='<?=base_path();?>/datas/applications/<?=apptitle($appname)?>/images/"+name+"'/>\n";
	if (name) {
	    oEditor.InsertHtml(html);
	}
    }
</script>

<form name="form1" action="index.php?appname=<?=$appname ?>&page=<?=$page?>" method="post" enctype="multipart/form-data" >
    <input type="Hidden" name="page" value="<? echo $page; ?>" />
    <input type="Hidden" name="datepage" value="<? echo $datepage; ?>" />
    <font size="2">Author</font> <input type="text" name="author" value="<?Echo "$author"; ?>"/>
    <font size=3>edit <b><? echo $page; ?></b></font>
    <hr />
    <?php
	if (file_exists("extras/FCKeditor/fckeditor.php")){
	    echo"<script>editor.Create();</script>";
	}
	else{
    ?>
	<textarea name='ta' cols='60' rows='15' ><?=$ta_text;?></textarea>
    <?php } ?>
	<table width='100%'>
	    <tr>
		<td>
		    <input type="submit" name="ok" value="Save" />
		    <input type="button" name="abort" value=" abort " onclick="location='index.php?appname=<?=$appname?>&page=<? echo $page; ?>';" />
		</td>
		<td>
		    <input type="button" name="ins" value="  insert html  " onclick="return insertHTML();" />
		    <input type="button" name="hil" value="  new Vwiki page " onclick="return newwikipage();" />
		</td>
		<td>
		    <select name="select1">
			<?
			    if ($hdir=opendir("datas/applications/".apptitle($appname)."/pages")) {
				while(($hfile=readdir($hdir))!=false){
				    if ($hfile[0]!="."){
					print("<option>$hfile</option>\n");
				    }
				}
			    }
			?>
		    </select>
		    <input type="button" name="link" value="link" onclick="return newlink();" />
		</td>
		<td>
		    <select name="select2" width='40'>
			<?
			    if ($hdir=opendir("datas/applications/".apptitle($appname)."/images")) {
				while(($hfile=readdir($hdir))!=false){
				    if ($hfile[0]!="."){
					print("<option>$hfile</option>\n");
				    }
				}
			    }
			?>
		    </select>
		    <input type="button" name="imageadd" value="addimage" onclick="return newimage();" />
		</td>
	    </tr>
	</table>
</form>

<form name='addfile' enctype='multipart/form-data' action='index.php?appname=<?=$appname?>&action=edit&page=<?=$page?>' method='post'>
    <p align='right'>
	upload file
	<input name='file' value="upload" type='file' size='40' />
	<input type='submit' value='upload'>
    </p>
</form>
